<?php
class lib_random
{
    static $aLetters = array(
            'n' => '23456789',
            'a' => 'abcdefhkmnpqstuvwxyz',
            'A' => 'ABCDEFGHKLMNPRSTUVWXYZ',
        );
        
    static function common_str($length)
    {
        $letters = implode('' , self::$aLetters);
        $letter_len = strlen($letters);
        $str = '';
        for ($i=0;$i<$length;$i++)
        {
            $str .= $letters{mt_rand(0,$letter_len-1)};
        }
        return $str;
    }
    
    
    /**
     * 按格式生成随机字串
     *
     * @param string $format        // n:number  A:upper    a:lower
     * @return string
     */
    static function formated_str($format = 'nnAnnAnn')
    {
        $string = '';
        $aLetterCnt = array(
            'n' => strlen(self::$aLetters['n']),
            'a' => strlen(self::$aLetters['a']),
            'A' => strlen(self::$aLetters['A']),
        );
        for ($i=0;$i<strlen($format);$i++)
        {
            $k = $format{$i};
            
            $iRnd = mt_rand(0,$aLetterCnt[$k]-1);
            
            $string .= self::$aLetters[$k]{$iRnd};
        }
        
        return $string;
    }
}
?>